<?php

namespace App\Service\ChatGroup\import;

use App\Models\ChatGroup\ChatGroupFAQModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class ChatFAQ
{
    /**
     * 导入聊天群组的常见问题
     * @param Collection $collection
     * @param Command|null $command
     * @return void
     */
    public function collection(Collection $collection, ?Command $command)
    {
        $num       = 0;
        $memoryArr = [];
        $collection->first()->map(function ($item) use (&$num, &$memoryArr) {
            if ($num == 0) {
                $num = 1;
                return;
            }
            $memoryArr[$item[0]][$item[1]][] = $item[2];
        });

        // 分别给分配ID
        $insert = [];

        foreach ($memoryArr as $one => $oneLevel) {
            $insert[$num] = [
                'id'    => $num,
                'desc'  => $one,
                'pid'   => 0,
                'level' => ChatGroupFAQModel::LEVEL_ZERO
            ];
            $id1 = $num;
            $num++;

            foreach ($oneLevel as $two => $twoLevel) {
                $id2 = $num;
                $insert[$num] = [
                    'id'    => $num,
                    'desc'  => $two,
                    'pid'   => $id1,
                    'level' => ChatGroupFAQModel::LEVEL_ONE
                ];
                $id3 = $num;
                $num++;

                foreach ($twoLevel as $threeLevel) {
                    $insert[$num] = [
                        'id'    => $num,
                        'desc'  => $threeLevel,
                        'pid'   => $id3,
                        'level' => ChatGroupFAQModel::LEVEL_TWO
                    ];
                    $num++;
                }
            }
        }
        foreach (array_chunk($insert, 30) as $value) {
            ChatGroupFAQModel::query()
                ->insert($value);
        }
    }

}
